package org.hgvc.webapp.ets.jsf.beans.member;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.DownPaymentInstallmentSchema;
import org.hgvc.webapp.ets.bo.InstallmentSchema;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.bo.Payment;
import org.hgvc.webapp.ets.bo.PaymentDtl;
import org.hgvc.webapp.ets.bo.SelectObj;
import org.hgvc.webapp.ets.bo.StandingInstruction;
import org.hgvc.webapp.ets.constant.AgreementConstant;
import org.hgvc.webapp.ets.constant.MemberConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;
import org.jocsb.base.util.SpringUtils;



public class MemberNewAgreementBean extends AbstractBean {

	public static final String BEAN_NAME="memberNewAgreementBean";
	private MemberService service;
	private MembershipService membershipService;
	private Member member;
	private List<Membership> membershipList;
	private String selectId;
	private Membership membership;
	private List<SelectObj> paymentOptionList;
	private String selectedPaymentOption;
	private double payAmt = 0;
	private double deposit = 0;
	private int month=0;
	private DownPaymentInstallmentSchema downPaymentSchema;
	private List<InstallmentSchema> schemaList;
	private MasterSetupService masterSetupService;
	private List<SelectItem> downPaymentSelectList;
	private List<DownPaymentInstallmentSchema> downPaymentList;
	private String downPaymentId;
	private double monthPay=0;
	private StandingInstruction instruction;
	private Agreement agreement;
	private PaymentDtl paymentDtl;
	private Payment payment;
	private String schemaId;
	private InstallmentSchema schema;
	private double deMonth = 0;
	private double deMonthPay = 0;
	
	public static final String CREATE_NEW="memberAddNewAgreement";
	public static final String SELECT_PAYENT_SCHEMA="memberSelectAgreementPayment";
	public static final String NAV_PAYMENT="memberAgreementConfirmPayment";
	public static final String NAV_INSTALLMENT_SELECT="memberSelectAgreementInstallmenSch";
	public static final String NAV_SELECT_DOWN_PAYMENT="memberSelectAgreementDownPayment";
	public static final String NAV_ACK_AGREEMENT="memberAgreementAck";
	
	
	
	public double getDeMonth() {
		return deMonth;
	}
	public void setDeMonth(double deMonth) {
		this.deMonth = deMonth;
	}
	public double getDeMonthPay() {
		return deMonthPay;
	}
	public void setDeMonthPay(double deMonthPay) {
		this.deMonthPay = deMonthPay;
	}
	public DownPaymentInstallmentSchema getDownPaymentSchema() {
		return downPaymentSchema;
	}
	public void setDownPaymentSchema(DownPaymentInstallmentSchema downPaymentSchema) {
		this.downPaymentSchema = downPaymentSchema;
	}
	public void setService(MemberService service) {
		this.service = service;
	}
	public void setMembershipService(MembershipService membershipService) {
		this.membershipService = membershipService;
	}
	public void setMasterSetupService(MasterSetupService masterSetupService) {
		this.masterSetupService = masterSetupService;
	}
	public double getPayAmt() {
		return payAmt;
	}
	public void setPayAmt(double payAmt) {
		this.payAmt = payAmt;
	}
	public Membership getMembership() {
		return membership;
	}
	public void setMembership(Membership membership) {
		this.membership = membership;
	}
	
	public String getSelectId() {
		return selectId;
	}
	public void setSelectId(String selectId) {
		this.selectId = selectId;
	}
	public double getDeposit() {
		return deposit;
	}
	public void setDeposit(double deposit) {
		this.deposit = deposit;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public double getMonthPay() {
		return monthPay;
	}
	public void setMonthPay(double monthPay) {
		this.monthPay = monthPay;
	}
	public StandingInstruction getInstruction() {
		return instruction;
	}
	public void setInstruction(StandingInstruction instruction) {
		this.instruction = instruction;
	}
	public Payment getPayment() {
		return payment;
	}
	public void setPayment(Payment payment) {
		this.payment = payment;
	}
	public String getDownPaymentId() {
		return downPaymentId;
	}
	public String navCreate(){
		if(member==null){
			MemberPortalBean bean = (MemberPortalBean)FacesUtils.getManagedBean(MemberPortalBean.BEAN_NAME);
			member = bean.getMember();
		}
		
		return CREATE_NEW;
	}
	public List<Membership> getMembershipList() {
		try{
			membershipList = membershipService.getMembershipList();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return membershipList;
	}
	
	public void selectMembershipListener(ActionEvent actionevent){
		UIParameter param = (UIParameter)actionevent.getComponent().findComponent("membershipId");
		selectId = (String)param.getValue();
		for(Membership mem:membershipList){
			if(mem.getMembershipId().equalsIgnoreCase(selectId)){
				this.membership = mem;
				break;
			}
		}
	}
	
	public String navSelectPaymentSchema(){
		return SELECT_PAYENT_SCHEMA;
	}
	
	public List<SelectObj> getPaymentOptionList() {

		SelectObj packageObj = new SelectObj();
		packageObj.setId(MemberConstant.MEMBER_PAYMENT_FULL_PACKAGE);
		packageObj.setTitle(SpringUtils.getMessage(MemberConstant.MEMBER_PAYMENT_FULL_PACKAGE, new Object[]{}));

		SelectObj installObj = new SelectObj();
		installObj.setId(MemberConstant.mEMBER_PAYMENT_INSTALLMET_SCHEMA);
		installObj.setTitle(SpringUtils.getMessage(MemberConstant.mEMBER_PAYMENT_INSTALLMET_SCHEMA, new Object[]{}));

		paymentOptionList = new ArrayList<SelectObj>();
		paymentOptionList.add(packageObj);
		paymentOptionList.add(installObj);

		return paymentOptionList;
	}
	
	public void paymentListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("paymentSelectId");
		selectedPaymentOption = (String)param.getValue();
	}
	
	public String navPaymentSelection(){
		if(selectedPaymentOption.equalsIgnoreCase(MemberConstant.MEMBER_PAYMENT_FULL_PACKAGE)){
			payAmt = membership.getMembershipFeeDbl();
			return NAV_PAYMENT;
		}else{
			return NAV_INSTALLMENT_SELECT;
		}
	}
	
	public String navInstallmentSelect(){
		return NAV_INSTALLMENT_SELECT;
	}
	
	public List<InstallmentSchema> getSchemaList() {
		try{
			schemaList = masterSetupService.getInstallmentSchemaList();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return schemaList;
	}
	
	public List<SelectItem> getSchemaSelectList(){
		List<SelectItem> list = new ArrayList<SelectItem>();
		for(InstallmentSchema sch:getSchemaList()){
			SelectItem item = new SelectItem(sch.getInstallmentSchemaId(),sch.getCode()+" "+sch.getName());
			list.add(item);
		}
		return list;
	}
	
	public String getSchemaId() {
		return schemaId;
	}
	public void setSchemaId(String schemaId) {
		try{
			this.schemaId = schemaId;
			if(schemaId!=null){
				for(InstallmentSchema sch:schemaList){
					if(sch.getInstallmentSchemaId().equalsIgnoreCase(schemaId)){
						double fee = membership.getMembershipFeeDbl();
						deposit = fee*(sch.getDepositPercentDbl()/100);
						monthPay = (fee - deposit)/sch.getMonthInt();
						monthPay = (Math.round(monthPay*100.0))/100.0;
						month = sch.getMonthInt();
						schema = sch;
						/*payAmt = deposit;*/
						break;
					}
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public String navSelectDownPaymentAction(){
		return NAV_SELECT_DOWN_PAYMENT;
	}
	
	public String removeDownPaymentAction(){
		downPaymentSchema = null;
		return NAV_INSTALLMENT_SELECT;
	}
	
	
	public List<SelectItem> getDownPaymentSelectList(){
		downPaymentSelectList = new ArrayList<SelectItem>();
		try{
			downPaymentList = masterSetupService.getDownPaymentInstallmentSchemaList();
			for(DownPaymentInstallmentSchema downPayment:downPaymentList){
				SelectItem item = new SelectItem(downPayment.getDownPaymentInstallmentSchemaId(),downPayment.getName());
				downPaymentSelectList.add(item);
			}
			
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return downPaymentSelectList;
	}
	
	public void setDownPaymentId(String downPaymentId) {
		this.downPaymentId = downPaymentId;
		for(DownPaymentInstallmentSchema sch:downPaymentList){
			if(sch.getDownPaymentInstallmentSchemaId().equalsIgnoreCase(downPaymentId)){
				deMonth = sch.getMonthInt();
				deMonthPay = deposit/deMonth;
				deMonthPay = (Math.round(deMonthPay*100.0))/100.0;
				downPaymentSchema = sch;
				break;
			}
		}
	}
	
	public void paymentSchemaActionListener(ActionEvent actionEvent){
		if(downPaymentSchema!=null){
			payAmt = monthPay;
		}else{
			payAmt = deposit;
		}
	}
	
	public String navSchemaCreatePayment(){
		instruction = new StandingInstruction();
		return NAV_PAYMENT;
	}
	
	public String confirmPaymentAction(){
		try{
			payment = new Payment();
			paymentDtl = new PaymentDtl();
			agreement = new Agreement();
			agreement.setMembership(this.membership);
			agreement.setRegType(AgreementConstant.AGREEMENT_NEW_ADD);
			if(schema==null&&downPaymentSchema==null){
				agreement.setPaymentType(AgreementConstant.AGREEMENT_PAYMENT_FULL_PACKAGE);
			}else if(schema!=null&&downPaymentSchema==null){
				agreement.setPaymentType(AgreementConstant.AGREEMENT_PAYMENT_INSTALLMENT_PACKAGE);
				agreement.setInstallmentSchema(schema);
			}else if(schema!=null&&downPaymentSchema!=null){
				agreement.setInstallmentSchema(schema);
				agreement.setDownPaymentInstallmentSchema(downPaymentSchema);
				agreement.setPaymentType(AgreementConstant.AGREEMENT_PAYMENT_INSTALLMENT_DEPOSIT_PACKAGE);
			}
			
			service.memberAddNewAgreement(member,instruction, getThisUser(), payment, paymentDtl, agreement, payAmt);
			
			return NAV_ACK_AGREEMENT;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return null;
	}
	
	public String backAction(){
		return MemberPortalBean.NAV_MEMBER_PORTAL;
	}
}
